<template>
    <div>
        <el-row>
            <el-col :span="24">
                <div>
                    <el-button type="primary" @click="addBtn">新增</el-button>

                    <el-button plain @click="TryPrintf">打印</el-button>

                </div>
            </el-col>
        </el-row>
        <br>
        <el-row>
            <el-col :span="24">
                <el-form :inline="true" :model="param">

                    <el-form-item label="" label-width="100px">
                        <el-input placeholder="请输入账号搜索" style="width: 200px; margin-left: 10px;"
                            v-model="param.username">
                        </el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="queryAllPowerUser">查询</el-button>
                        <el-button style="margin-left: 10px;" @click="clearSearch">重置</el-button>
                    </el-form-item>

                </el-form>
            </el-col>
        </el-row>


        <el-row>
            <el-col :span="24">
                <div>
                    <el-table :data="tableData" style="width: 100%; margin-top: 20px;"
                        @selection-change="handleSelectionChange">
                        <el-table-column type="selection" width="55">
                        </el-table-column>
                        <el-table-column prop="id" label="用户ID" width="200"></el-table-column>

                        <el-table-column prop="username" label="用户账号" width="250"></el-table-column>

                        <el-table-column prop="realName" label="真实姓名" width="250"
                            :formatter="(row) => row.realName ? row.realName : '保密'">
                        </el-table-column>

                        <el-table-column prop="locked" label="用户状态" width="250">
                            <template slot-scope="scope">
                                <span v-if="scope.row.locked == 0" style="color: green;">正常</span>
                                <span v-else-if="scope.row.locked == 1" style="color: red;">封禁</span>
                            </template>
                        </el-table-column>

                        <el-table-column prop="vip" label="vip积分" width="200"></el-table-column>

                        <el-table-column prop="createdTime" label="创建时间" width="250"></el-table-column>

                        <el-table-column label="操作" width="200">
                            <template slot-scope="scope">
                                <el-button type="text" size="medium" style="color: red;"
                                    @click="deleteBtn(scope.row.id)">封禁</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <!-- 分页部分 -->
                <div>
                    <el-pagination background :page-sizes="[5, 10, 20, 50]" :page-size="pageSize"
                        :current-page="pageNum" layout="total, sizes, prev, pager, next, jumper" :total="total"
                        @prev-click="prePage" @next-click="nextPage" @current-change="curPage"
                        @size-change="sizeChange">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>



        <!-- 添加弹窗 -->
        <el-dialog title="新增" :visible.sync="addVisible">
            <el-form>
                <el-form-item label="账号" label-width="80px">
                    <el-input v-model="add.username"></el-input>
                </el-form-item>

                <el-form-item label="密码" label-width="80px">
                    <el-input v-model="add.password"></el-input>
                </el-form-item>

                <el-form-item label="姓名" label-width="80px">
                    <el-input v-model="add.realName"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
export default {
    data() {
        return {
            pageSize: 10,
            pageNum: 1,
            total: 0,

            tableData: [],
            param: {},
            multipleSelection: [],

            addVisible: false,
            add: {}
        }
    },

    methods: {
        TryPrintf() {
            window.print();  // 调用浏览器打印功能
        },
        handleSelectionChange(val) {
            //点击复选框的事件val：是选中的对象数组
            this.multipleSelection = val;
        },

        queryAllPowerUser() {
            let pageParam = {};
            pageParam.pageNum = this.pageNum;
            pageParam.pageSize = this.pageSize;
            pageParam.param = this.param;
            this.$axios.post('api/powerUser/queryAllPowerUser', pageParam)
                .then(resp => {
                    console.log(resp.data);
                    if (resp.data.code == 200) {
                        this.tableData = resp.data.data.list;
                        this.total = resp.data.data.total;
                    }
                })
        },

        clearSearch() {
            this.param = {};
            this.queryAllPowerUser();
        },

        deleteBtn(id) {
            this.$axios.get('api/powerUser/del/' + id)
                .then(response => {
                    if (response.data.code == 200) {
                        this.queryAllPowerUser();
                    }
                })
        },

        addBtn() {
            this.addVisible = true;
        },
        saveAdd() {
            this.$axios.post('api/powerUser/addPowerUser', this.add)
                .then(resposne => {
                    if (resposne.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '成功!'
                        });
                        this.addVisible = false;
                        this.queryAllPowerUser();
                    }
                })
        },

        //--------------------------------------------------------------

        // 点击上一页
        prePage() {
            if (this.pageNum > 1) {
                this.pageNum--;
                this.queryAllPowerUser(); // 更新数据
            }
        },

        // 点击下一页
        nextPage() {
            if (this.pageNum < Math.ceil(this.total / this.pageSize)) {
                this.pageNum++;
                this.queryAllPowerUser(); // 更新数据
            }
        },

        // 页码变化
        curPage(pageNum) {
            this.pageNum = pageNum;
            this.queryAllPowerUser(); // 更新数据
        },

        // 每页显示条数变化
        sizeChange(pageSize) {
            this.pageSize = pageSize;
            this.pageNum = 1; // 每页条数变化时，回到第一页
            this.queryAllPowerUser(); // 更新数据
        },


        // ------------------------------------------------------------------
    },
    created() {
        let userInfoString = localStorage.getItem('userInfo');
        let userInfo = JSON.parse(userInfoString);
        let userIds = userInfo.id;

        this.queryAllPowerUser();
    }
}
</script>

<style scoped></style>
